package com.owc.common.enums;

import java.util.Arrays;
import java.util.Optional;

public enum ClientEnum {
    WEB(0,DecodeEnum.BASE64,"web端"),
    IOS(1,DecodeEnum.XXTEA,"IOS"),
    ANDROID(2,DecodeEnum.XXTEA,"ANDROID");

    ClientEnum(Integer type, DecodeEnum decodeEnum, String desc) {
        this.type = type;
        this.decodeEnum = decodeEnum;
        this.desc = desc;
    }

    private Integer type;

    private DecodeEnum decodeEnum;

    private String desc;

    public Integer getType() {
        return type;
    }

    public DecodeEnum getDecodeEnum() {
        return decodeEnum;
    }

    public String getDesc() {
        return desc;
    }

    /**
     * Find client enum by client type.
     * @param type type
     * @return Client enum example.
     */
    public static Optional<ClientEnum> match(Integer type){
        return Arrays.stream(ClientEnum.values())
                .filter(t -> t.getType().equals(type))
                .findFirst();
    }

    public static Optional<ClientEnum> matchByDesc(String desc){
        return Arrays.stream(ClientEnum.values())
                .filter(t -> t.getDesc().equals(desc))
                .findFirst();
    }

    /**
     * Find string decode handler by client type. If the match fail. Return default decode handler(WEB way).
     * @param type client type
     * @return decode handler interface
     */
    public static DecodeEnum findDecode(Integer type){
        return match(type).orElse(WEB).getDecodeEnum();
    }
}
